﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;
using System.Web.UI;

namespace SR.API
{
    public class NivoTemplate :ITemplate
    {  
        public void InstantiateIn(System.Web.UI.Control container)
        {
            PlaceHolder ph = new PlaceHolder();
            ph.DataBinding += new EventHandler(Item_DataBinding);
            container.Controls.Add(ph);
        }

        static void Item_DataBinding(object sender, System.EventArgs e)
        {
            PlaceHolder ph = (PlaceHolder)sender;
                
            RepeaterItem ri = (RepeaterItem)ph.NamingContainer;
        
            Image image = new Image();

            image.ImageUrl = (String)DataBinder.Eval(ri.DataItem, "ImageUrl");
            image.AlternateText = (String)DataBinder.Eval(ri.DataItem, "ImageName");
            image.Attributes.Add("title", (String)DataBinder.Eval(ri.DataItem, "ImageName"));
            if ((String)DataBinder.Eval(ri.DataItem, "SliderTransitionEffect") != "random")
            {
                image.Attributes.Add("data-transition", (String)DataBinder.Eval(ri.DataItem, "SliderTransitionEffect"));
            }
            
            string link = (String)DataBinder.Eval(ri.DataItem, "LinkUrl");
            
            if (!link.IsNullOrEmpty())
            {
                HyperLink imageLink = new HyperLink();
                imageLink.NavigateUrl = link;
                imageLink.Target = "_blank";
                imageLink.Controls.Add(image);
                ph.Controls.Add(imageLink);
            }
            else
                ph.Controls.Add(image);
        }
    }
}
